﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Wicresoft.SMS.Common;
using Wicresoft.SMS.Data;

namespace Wicresoft.SMS.Gateway.Host
{
    public class QueryMsgTask : Task
    {
        protected ISMSProvider _smsProvider;
        protected IServiceConfigProvider _configProvider;
        protected IDataProvider _databaseProvider;

        public QueryMsgTask(
            ISMSProvider smsProvider,
            IServiceConfigProvider configProvider,
            IDataProvider databaseProvider
            )
        {
            _smsProvider = smsProvider;
            _configProvider = configProvider;
            _databaseProvider = databaseProvider;
        }
        protected override int TaskEnable
        {
            get
            {
                return _configProvider.QueryMsgTaskEnable;
            }
            set 
            {
                TaskEnable = value;
            }
        }
        protected override int TaskSpan
        {
            get
            {
                return _configProvider.QueryMsgTaskSpan;
            }
            set 
            {
                TaskSpan = value;
            }
        }
        protected override void ExecuteCore()
        {
            while (_scanAble && TaskEnable == 1)
            {
                ScanSendingCore();
                System.Threading.Thread.Sleep(
                    TaskSpan < GlobalFactory.TaskSpanBorder ? GlobalFactory.TaskSpanBorder : _configProvider.QueryMsgTaskSpan);
            }
        }
        private void ScanSendingCore()
        {
            DBLog.Debug("Enter ScanSendingCore");
            var msgs = _databaseProvider.GetMsgSending();
            if (msgs == null)
            {
                return;
            }
            List<KeyandMessageId> changeSuccessInfo = new List<KeyandMessageId>();
            List<KeyandMessageId> changeFailedInfo = new List<KeyandMessageId>();
            foreach (var msg in msgs)
            {
                MsgResult msgResult = _smsProvider.QueryMsgState(msg.MessageId);
                if (msgResult.status == MsgStates.Success)
                {
                    KeyandMessageId info = new KeyandMessageId();
                    info.MsgIdInDbPrimaryKey = msg.MsgIdInDbPrimaryKey;
                    info.MessageId = msg.MessageId;
                    changeSuccessInfo.Add(info);
                }
                else if (msgResult.status == MsgStates.Fail)
                {
                    KeyandMessageId info = new KeyandMessageId();
                    info.MsgIdInDbPrimaryKey = msg.MsgIdInDbPrimaryKey;
                    info.MessageId = null;
                    changeFailedInfo.Add(info);


                }

                DBLog.Debug("ScanSendingCore MsgIdInDbPrimaryKey:" + msg.MsgIdInDbPrimaryKey);
            }
            List<Email> pushEmail1 = new List<Email>();
            pushEmail1 = _databaseProvider.SetMsgSendingSuccess(changeSuccessInfo);
            List<Email> pushEmail2 = new List<Email>();
            pushEmail2 = _databaseProvider.SetMsgSendingFailed(changeFailedInfo);
            var sqlEmails = GlobalFactory.CreateEmailDatabaseProvider();
            if (pushEmail1 != null && pushEmail1.Count() != 0)
            {
                if (pushEmail2 != null && pushEmail2.Count() != 0)
                    pushEmail1.AddRange(pushEmail2);
                foreach (var email in pushEmail1)
                {
                    string mail = email.Content.Replace("tobechange", _configProvider.WebUrl);
                    mail = mail.Replace("leftcall", "{");
                    email.Content = mail.Replace("rightcall", "}");
                }
                sqlEmails.SendEmail(pushEmail1);
            }
            else if (pushEmail2 != null && pushEmail2.Count() != 0)
            {
                if (pushEmail1 != null && pushEmail1.Count() != 0)
                    pushEmail2.AddRange(pushEmail1);
                foreach (var email in pushEmail2)
                {
                    string mail = email.Content.Replace("tobechange", _configProvider.WebUrl);
                    mail = mail.Replace("leftcall", "{");
                    email.Content = mail.Replace("rightcall", "}");
                }
                sqlEmails.SendEmail(pushEmail2);
            }

            DBLog.Debug("Leaving ScanSendingCore");
        }
    }
}
